﻿@inherits RadzenComponent

@if (Visible)
{
    <div @ref="@Element" @attributes="Attributes" class="@GetCssClass()" style="@Style" id="@GetId()">
        <div class="rz-form">
            <div class="row form-group">
                <label class="col-sm-3 col-form-label" for="username">@UserText</label>
                <div class="col">
                    <RadzenTextBox AutoComplete=@AutoComplete style="display: block" Name="Username" @bind-Value=@Username />
                    <RadzenRequiredValidator Component="Username" Text=@UserRequired style="position: absolute" />
                </div>
            </div>
            <div class="row form-group">
                <label class="col-sm-3 col-form-label" for="password">@PasswordText</label>
                <div class="col">
                    <RadzenPassword AutoComplete=@AutoComplete style="display: block" Name="Password" @bind-Value=@Password />
                    <RadzenRequiredValidator Component="Password" Text=@PasswordRequired style="position: absolute" />
                </div>
            </div>
            <div class="row form-group">
                <div class="offset-sm-3 col login-buttons">
                    <RadzenButton ButtonStyle="ButtonStyle.Primary" ButtonType="ButtonType.Submit" Text=@LoginText Click=@OnLogin />
                    @if (AllowResetPassword)
                    {
                        <a @onclick=@OnReset>@ResetPasswordText</a>
                    }
                </div>
            </div>
        </div>
        @if (AllowRegister)
        {
            <div class="register">
                @RegisterMessageText
                <RadzenButton ButtonType="ButtonType.Button" ButtonStyle="ButtonStyle.Primary" Text=@RegisterText Click=@OnRegister />
            </div>
        }
    </div>
}
@code {  
    [Parameter]
    public bool AutoComplete { get; set; } = true;

    protected override string GetComponentCssClass()
    {
        return "login";
    }

    string _username;
    [Parameter]
    public string Username
    {
        get
        {
            return _username;
        }
        set
        {
            if (_username != value)
            {
                _username = value;
            }
        }
    }

    string _password;
    [Parameter]
    public string Password
    {
        get
        {
            return _password;
        }
        set
        {
            if (_password != value)
            {
                _password = value;
            }
        }
    }

    [Parameter]
    public EventCallback<Radzen.LoginArgs> Login { get; set; }

    [Parameter]
    public EventCallback Register { get; set; }

    [Parameter]
    public EventCallback<string> ResetPassword { get; set; }

    [Parameter]
    public bool AllowRegister { get; set; } = true;

    [Parameter]
    public bool AllowResetPassword { get; set; } = true;

    [Parameter]
    public string LoginText { get; set; } = "Login";

    [Parameter]
    public string RegisterText { get; set; } = "Sign up";

    [Parameter]
    public string RegisterMessageText { get; set; } = "Don't have an account yet?";

    [Parameter]
    public string ResetPasswordText { get; set; } = "Forgot password";

    [Parameter]
    public string UserText { get; set; } = "Username";

    [Parameter]
    public string UserRequired { get; set; } = "Username is required";

    [Parameter]
    public string PasswordText { get; set; } = "Password";

    [Parameter]
    public string PasswordRequired { get; set; } = "Password is required";

    protected async Task OnLogin()
    {
        if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password))
        {
            await Login.InvokeAsync(new Radzen.LoginArgs { Username = Username, Password = Password });
        }
    }

    protected async Task OnReset(EventArgs args)
    {
        if (!string.IsNullOrEmpty(Username))
        {
            await ResetPassword.InvokeAsync(Username);
        }
    }

    protected async Task OnRegister()
    {
        await Register.InvokeAsync(EventArgs.Empty);
    }
}